// 引入相关模块资源
const express = require('express');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const path = require('path');

// 引入相关接口文件
const login = require('./modules/user/login');
const logout = require('./modules/user/logout');
const isLogin = require('./modules/user/isLogin');
const register = require('./modules/user/register');
const getNews = require('./modules/user/getNews');
const getModule = require('./modules/user/getModule');
const getNewsList = require('./modules/user/getNewsList');
const getComment = require('./modules/user/getComment');
const createComment = require('./modules/user/createComment');
const setUser = require('./modules/user/setUser');
const changePassword = require('./modules/user/changePassword');

const adminIsLogin = require('./modules/admin/isLogin');
const adminLogin = require('./modules/admin/login');
const adminLogout = require('./modules/admin/logout');

const adminGetModule = require('./modules/admin/getModule');
const adminGetNews = require('./modules/admin/getNews');
const adminSetNews = require('./modules/admin/setNews');

const createNews = require('./modules/admin/createNews');
const createModule = require('./modules/admin/createModule');
const setModule = require('./modules/admin/setModule');
const getUser = require('./modules/admin/getUser');
const adminSetUser = require('./modules/admin/setUser');
const adminGetComment = require('./modules/admin/getComment');
const setComment = require('./modules/admin/setComment');
const setAdmin = require('./modules/admin/setAdmin');
const adminChangePassword = require('./modules/admin/changePassword');

const uploader = require('./modules/tools/upload');
const multer = require('multer');//接收图片
const upload = multer({
    dest: '/public/assets'
});//定义图片上传的临时目录




// 创建服务器
const app = express();

// 设置静态资源
app.use(express.static('public'));

// 对请求体进行格式化
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

// 对请求加cookie
app.use(cookieSession({
  name: 'session',
  keys: ['news','system'],
  maxAge: 24 * 60 * 60 * 1000 
}))

// 处理接口
app.get('/api/isLogin', isLogin);
app.post('/api/login', login);
app.post('/api/logout', logout);
app.post('/api/register', register);
app.post('/api/getNews', getNews);
app.post('/api/getNewsList', getNewsList);
app.post('/api/getComment',getComment);
app.post('/api/getModule',getModule);
app.post('/api/createComment',createComment);
app.post('/api/setUser',setUser);
app.post('/api/changePassword',changePassword);

app.get('/api/admin/isLogin', adminIsLogin);
app.post('/api/admin/login', adminLogin);
app.post('/api/admin/logout', adminLogout);


app.get('/api/admin/getNews', adminGetNews);
app.post('/api/admin/setNews', adminSetNews);
app.get('/api/admin/getModule', adminGetModule);
app.post('/api/admin/createNews', createNews);
app.post('/api/admin/createModule', createModule);
app.post('/api/admin/setModule', setModule);
app.post('/api/admin/getUser', getUser);
app.post('/api/admin/setUser', adminSetUser);
app.post('/api/admin/getComment', adminGetComment);
app.post('/api/admin/setComment', setComment);
app.post('/api/admin/setAdmin', setAdmin);
app.post('/api/admin/changePassword', adminChangePassword);




// 单域单文件上传
app.post('/api/upload', upload.single('imgfile'), uploader)



const server = app.listen(8000, ()=>{
  let host = server.address().address;
  let port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});